package org.codeart.juc.future;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

public class FutureTaskDemo {

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ExecutorService pool = Executors.newFixedThreadPool(10);
        try {
            // 构建FutureTask
            FutureTask<String> task = new FutureTask<>(() -> {
                System.out.println("任务开始执行...");
                Thread.sleep(2000);
                System.out.println("任务执行完毕...");
                return "OK!";
            });
            pool.execute(task);
            System.out.println("任务结果: " + task.get());
        } finally {
            pool.shutdown();
        }
    }

}
